using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using System.IO;
using System.Web;
using System.Data.SqlTypes;
using ECommerce.DAL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using eShop.Model;
using eShop.Business;

namespace AutoGear.BLL
{
    public class RawData
    {

        private static ReadWriteCustomCollection<Manufacturer> _productBrand;

        public static bool InsertToProduct(TblExportImportProduct data, string tempImageFolder)
        {
            if (!data.IsInserted)
            {
                string name = data.Name;
                string categoryId = CreateCategory(data).ToString();
                string brandId = CreateManufacturer(data).ToString();
                string price = data.Price.ToString();
                string itemNumber = data.ItemNumber;
                string displayOrder = data.DisplayOrder.ToString();
                bool featuredListing = data.Featured;
                bool active = data.Active;
                string description = data.Description;
                double weight = Convert.ToDouble(data.Weight);
                int stockAmount = data.StockAmount;
                double currentCost = Convert.ToDouble(data.CurrentCost);
                double standardCost = Convert.ToDouble(data.StandardCost);
                int pkgQuantity = data.PackageQuanity;
                bool useDecimalForSize = data.UseDecimalForSize;

                double widthFraction = 0.0;
                if (data.Width4 > 0)
                {
                    widthFraction = ((double)data.Width3 / data.Width4);
                }
                double widthValue = (data.Width1 * 10 + data.Width2) + widthFraction;

                double diaFraction = 0.0;
                if (data.Diameter4 > 0)
                {
                    diaFraction = ((double)data.Diameter3 / data.Diameter4);
                }
                double diameterValue = (data.Diameter1 * 10 + data.Diameter2) + diaFraction;

                double threadFraction = 0.0;
                if (data.ThreadCount4 > 0)
                {
                    threadFraction = ((double)data.ThreadCount3 / data.ThreadCount4);
                }

                double threadCountValue = (data.ThreadCount1 * 10 + data.ThreadCount2) + threadFraction;

                double lengthFraction = 0.0;
                if (data.Length4 > 0)
                {
                    lengthFraction = ((double)data.Length3 / data.Length4);
                }

                double lengthValue = (data.Length1 * 10 + data.Length2) + lengthFraction;

                try
                {
                    ProductStock stock = new ProductStock(0, data.SizeName, Convert.ToDouble(data.Weight),
                        string.Empty, data.Width1, data.Width2, data.Width3, data.Width4, widthValue, string.Empty,
                        data.Diameter1, data.Diameter2, data.Diameter3, data.Diameter4, diameterValue, string.Empty,
                        data.ThreadCount1, data.ThreadCount2, data.ThreadCount3, data.ThreadCount4, threadCountValue, string.Empty,
                        data.Length1, data.Length2, data.Length3, data.Length4, lengthValue, string.Empty,
                        data.StockAmount, 0, data.PackageQuanity, useDecimalForSize);

                    List<ProductStock> stocks = new List<ProductStock>();
                    stocks.Add(stock);

                    Product newProduct = Product.CreateProduct(name,string.Empty, description, tempImageFolder,
                        categoryId, brandId, displayOrder, price, currentCost, currentCost,string.Empty,string.Empty, string.Empty, standardCost, false, itemNumber, featuredListing, active, stocks);

                    data.IsInserted = true;
                    TblExportImportProduct.UpdateTblExportImportProduct(data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool UpdateToProduct(TblExportImportProduct data, string tempImageFolder)
        {
            if (!data.IsInserted)
            {
                try
                {
                    Product product = Product.GetProductByItemNumber(data.ItemNumber);
                    if (product == null)
                        return false;

                    product.ProductName = data.Name;
                    product.Category = CategoryManager.Load(CreateCategory(data));
                    product.Brand = ManufacturerManager.GetById(CreateManufacturer(data));
                    product.RetailPrice = product.OldPrice = product.ProductPrice = Convert.ToDouble(data.Price);
                    product.DisplayOrder = data.DisplayOrder.ToString();
                    product.IsFeatured = data.Featured;
                    product.IsActive = data.Active;
                    product.ProductDescription = data.Description;
                    product.CurrentCost = Convert.ToDouble(data.CurrentCost);
                    product.StandardCost = Convert.ToDouble(data.StandardCost);

                    #region Stock Update
                    ProductStock stock = product.Stocks[0];

                    #region Stock Calculation Related To Dimensions
                    double widthFraction = 0.0;
                    if (data.Width4 > 0)
                    {
                        widthFraction = ((double)data.Width3 / data.Width4);
                    }
                    double widthValue = (data.Width1 * 10 + data.Width2) + widthFraction;

                    double diaFraction = 0.0;
                    if (data.Diameter4 > 0)
                    {
                        diaFraction = ((double)data.Diameter3 / data.Diameter4);
                    }
                    double diameterValue = (data.Diameter1 * 10 + data.Diameter2) + diaFraction;

                    double threadFraction = 0.0;
                    if (data.ThreadCount4 > 0)
                    {
                        threadFraction = ((double)data.ThreadCount3 / data.ThreadCount4);
                    }
                    double threadCountValue = (data.ThreadCount1 * 10 + data.ThreadCount2) + threadFraction;

                    double lengthFraction = 0.0;
                    if (data.Length4 > 0)
                    {
                        lengthFraction = ((double)data.Length3 / data.Length4);
                    }
                    double lengthValue = (data.Length1 * 10 + data.Length2) + lengthFraction;
                    #endregion

                    stock.Size = data.SizeName;
                    stock.Weight = Convert.ToDouble(data.Weight);
                    
                    stock.WidthWholeNumberFirstDigit = data.Width1;
                    stock.WidthWholeNumberSecondDigit = data.Width2;
                    stock.WidthFractionNumberFirstDigit = data.Width3;
                    stock.WidthFractionNumberSecondDigit = data.Width4;
                    stock.WidthValue = widthValue;
                    stock.WidthUnit = string.Empty;

                    stock.DiameterWholeNumberFirstDigit = data.Diameter1;
                    stock.DiameterWholeNumberSecondDigit = data.Diameter2;
                    stock.DiameterFractionNumberFirstDigit = data.Diameter3;
                    stock.DiameterFractionNumberSecondDigit = data.Diameter4;
                    stock.DiameterValue = diameterValue;
                    stock.DiameterUnit = string.Empty;

                    stock.ThreadCountWholeNumberFirstDigit = data.ThreadCount1;
                    stock.ThreadCountWholeNumberSecondDigit = data.ThreadCount2;
                    stock.ThreadCountFractionNumberFirstDigit = data.ThreadCount3;
                    stock.ThreadCountFractionNumberSecondDigit = data.ThreadCount4;
                    stock.ThreadCountValue = threadCountValue;
                    stock.ThreadCountUnit = string.Empty;

                    stock.LengthWholeNumberFirstDigit = data.Length1;
                    stock.LengthWholeNumberSecondDigit = data.Length2;
                    stock.LengthFractionNumberFirstDigit = data.Length3;
                    stock.LengthFractionNumberSecondDigit = data.Length4;
                    stock.LengthValue = lengthValue;
                    stock.LengthUnit = string.Empty;

                    stock.StockAmount = data.StockAmount;
                    stock.PkgQuanity = data.PackageQuanity;
                    stock.UseDecimalForSize = data.UseDecimalForSize;
                    #endregion

                    product.Update();

                    data.IsInserted = true;
                    TblExportImportProduct.UpdateTblExportImportProduct(data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void InsertToRawData(Product product)
        {
            try
            {
                ReadWriteCustomCollection<ProductStock> stocks = ProductStock.GetStockByProductId(product.ProductId);

                TblExportImportProduct data = new TblExportImportProduct();
                data.Name =  product.ProductName;
                data.Active = product.IsActive;
                data.CurrentCost = Convert.ToDecimal(product.CurrentCost);
                data.Description = System.Web.HttpUtility.HtmlDecode(product.ProductDescription);
                data.DisplayOrder = Convert.ToInt32(product.DisplayOrder);
                data.Featured = product.IsFeatured;
                data.ImageName = product.ProductImageUrl;
                data.IsInserted = true;
                data.ItemNumber = product.ItemNumber;
                data.Manufacturer = product.Brand.BrandName;
                data.Price = Convert.ToDecimal(product.ProductActualPrice);
                data.StandardCost = Convert.ToDecimal(product.StandardCost);

                Stack<Category> categories = GetCategorySequence(product.Category);
                if (categories.Count == 1)
                {
                    data.Category = categories.Pop().CategoryName;
                }
                else if (categories.Count == 2)
                {
                    data.Category = categories.Pop().CategoryName;
                    data.SubCategory1 = categories.Pop().CategoryName;
                }
                else if (categories.Count == 3)
                {
                    data.Category = categories.Pop().CategoryName;
                    data.SubCategory1 = categories.Pop().CategoryName;
                    data.SubCategory2 = categories.Pop().CategoryName;
                }
                else if (categories.Count == 4)
                {
                    data.Category = categories.Pop().CategoryName;
                    data.SubCategory1 = categories.Pop().CategoryName;
                    data.SubCategory2 = categories.Pop().CategoryName;
                    data.SubCategory3 = categories.Pop().CategoryName;
                }
                else if (categories.Count == 5)
                {
                    data.Category = categories.Pop().CategoryName;
                    data.SubCategory1 = categories.Pop().CategoryName;
                    data.SubCategory2 = categories.Pop().CategoryName;
                    data.SubCategory3 = categories.Pop().CategoryName;
                    data.SubCategory4 = categories.Pop().CategoryName;
                }
                else if (categories.Count == 6)
                {
                    data.Category = categories.Pop().CategoryName;
                    data.SubCategory1 = categories.Pop().CategoryName;
                    data.SubCategory2 = categories.Pop().CategoryName;
                    data.SubCategory3 = categories.Pop().CategoryName;
                    data.SubCategory4 = categories.Pop().CategoryName;
                    data.SubCategory5 = categories.Pop().CategoryName;
                }

                if (stocks != null && stocks.Count > 0)
                {
                    data.Diameter1 = stocks[0].DiameterWholeNumberFirstDigit;
                    data.Diameter2 = stocks[0].DiameterWholeNumberSecondDigit;
                    data.Diameter3 = stocks[0].DiameterFractionNumberFirstDigit;
                    data.Diameter4 = stocks[0].DiameterFractionNumberSecondDigit;
                    data.Length1 = stocks[0].LengthWholeNumberFirstDigit;
                    data.Length2 = stocks[0].LengthWholeNumberSecondDigit;
                    data.Length3 = stocks[0].LengthFractionNumberFirstDigit;
                    data.Length4 = stocks[0].LengthFractionNumberSecondDigit;
                    data.PackageQuanity = stocks[0].PkgQuanity;
                    data.SizeName = stocks[0].Size;
                    data.StockAmount = stocks[0].StockAmount;
                    data.ThreadCount1 = stocks[0].ThreadCountWholeNumberFirstDigit;
                    data.ThreadCount2 = stocks[0].ThreadCountWholeNumberSecondDigit;
                    data.ThreadCount3 = stocks[0].ThreadCountFractionNumberFirstDigit;
                    data.ThreadCount4 = stocks[0].ThreadCountFractionNumberSecondDigit;
                    data.UseDecimalForSize = stocks[0].UseDecimalForSize;
                    data.Weight = Convert.ToDecimal(stocks[0].Weight);
                    data.Width1 = stocks[0].WidthWholeNumberFirstDigit;
                    data.Width2 = stocks[0].WidthWholeNumberSecondDigit;
                    data.Width3 = stocks[0].WidthFractionNumberFirstDigit;
                    data.Width4 = stocks[0].WidthFractionNumberSecondDigit;
                    data.Save();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static Stack<Category> GetCategorySequence(Category productCategory)
        {
            Stack<Category> categories = new Stack<Category>();
            do
            {
                categories.Push(productCategory);
                productCategory = productCategory.ParentCategory;
            }
            while (productCategory.CategoryId != SiteUtility.SuperRootCategoryId);

            return categories;
        }

        //public static void InsertToRawData(Product product)
        //{
        //    try
        //    {
        //        ReadWriteCustomCollection<ProductStock> stocks = ProductStock.GetStockByProductId(product.ProductId);

        //        TblRawData data = new TblRawData();
        //        data.ItemDescription = product.ProductName;
        //        data.ItemClassCode = product.Category.ParentCategory.CategoryName;
        //        data.SubCat1 = product.Category.CategoryName;
        //        data.Manufacturer = product.Brand.BrandName;
        //        data.PriceLevel = Convert.ToDecimal(product.ProductPrice);
        //        data.ItemNumber = product.ItemNumber;
        //        data.CurrentCost = Convert.ToDecimal(product.CurrentCost);
        //        data.StandardCost = Convert.ToDecimal(product.StandardCost);
        //        data.DefaultOrder = product.DisplayOrder;
        //        data.IsInserted = true;
        //        try
        //        {
        //            if (stocks != null && stocks.Count > 0)
        //            {
        //                product.AddStock(stocks[0]);
        //                data.PkgQuanity = product.Stocks[0].PkgQuanity;
        //                data.QTYOnHand = product.Stocks[0].StockAmount;
        //                data.ItemWeight = Convert.ToDecimal(product.Stocks[0].Weight);
        //                data.ItemShortName = product.Stocks[0].Size;
        //                data.WidthWholeNumberFirstDigit = product.Stocks[0].WidthWholeNumberFirstDigit;
        //                data.WidthWholeNumberSecondDigit = product.Stocks[0].WidthWholeNumberSecondDigit;
        //                data.WidthFractionNumberFirstDigit = product.Stocks[0].WidthFractionNumberFirstDigit;
        //                data.WidthFractionNumberSecondDigit = product.Stocks[0].WidthFractionNumberSecondDigit;
        //                data.DiameterWholeNumberFirstDigit = product.Stocks[0].DiameterWholeNumberFirstDigit;
        //                data.DiameterWholeNumberSecondDigit = product.Stocks[0].DiameterWholeNumberSecondDigit;
        //                data.DiameterFractionNumberFirstDigit = product.Stocks[0].DiameterFractionNumberFirstDigit;
        //                data.DiameterFractionNumberSecondDigit = product.Stocks[0].DiameterFractionNumberSecondDigit;
        //                data.ThreadCountWholeNumberFirstDigit = product.Stocks[0].ThreadCountWholeNumberFirstDigit;
        //                data.ThreadCountWholeNumberSecondDigit = product.Stocks[0].ThreadCountWholeNumberSecondDigit;
        //                data.ThreadCountFractionNumberFirstDigit = product.Stocks[0].ThreadCountFractionNumberFirstDigit;
        //                data.ThreadCountFractionNumberSecondDigit = product.Stocks[0].ThreadCountFractionNumberSecondDigit;
        //                data.LengthWholeNumberFirstDigit = product.Stocks[0].LengthWholeNumberFirstDigit;
        //                data.LengthWholeNumberSecondDigit = product.Stocks[0].LengthWholeNumberSecondDigit;
        //                data.LengthFractionNumberFirstDigit = product.Stocks[0].LengthFractionNumberFirstDigit;
        //                data.LengthFractionNumberSecondDigit = product.Stocks[0].LengthFractionNumberSecondDigit;
        //            }
        //            else
        //            {
        //                data.PkgQuanity = 0;
        //                data.QTYOnHand = 0;
        //                data.ItemWeight = Convert.ToDecimal(0.0);
        //            }
        //        }
        //        catch
        //        {
        //            data.PkgQuanity = 0;
        //            data.QTYOnHand = 0;
        //            data.ItemWeight = Convert.ToDecimal(0.0);
        //        }
        //        data.Save();
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        public static void ExportAllToExcel(string FilePath)
        {
            try
            {
                ECommerce.DAL.CustomCollection<TblExportImportProduct> rows = TblExportImportProduct.GetAllTblExportImportProduct();
                ExportToExcel(rows, FilePath);
            }
            catch (Exception ex) { throw ex; }
        }

        public static void ExportFailedToExcel(string FilePath)
        {
            try
            {
                ECommerce.DAL.CustomCollection<TblExportImportProduct> rows = TblExportImportProductExtended.GetExportImportProductByIsInserted(false);
                ExportToExcel(rows, FilePath);
            }
            catch (Exception ex) { throw ex; }
        }

        public static void ExportToExcel(ECommerce.DAL.CustomCollection<TblExportImportProduct> rows, string FilePath)
        {
            try
            {
                //FileStream fileStream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
                //ECommerce.DAL.CustomCollection<TblRawData> rows = TblRawData.GetAllTblRawData();
                //StreamWriter streamWriter = new StreamWriter(fileStream);
                //string strLine;
                //for (int i = 0; i < rows.Count; i++)
                //{
                //    TblRawData row = rows[i];
                //    strLine = string.Empty;
                //    strLine = row.ItemNumber + "\t" + row.QTYOnHand + "\t" + row.ItemDescription + "\t" + row.CurrentCost + "\t" + row.StandardCost + "\t" + row.Manufacturer + "\t" + row.PriceLevel + "\t" + row.PkgQuanity + "\t" + row.SellingUOfM + "\t" + row.ItemClassCode + "\t" + row.QTYAllocated + "\t" + row.QTYAvailable + "\t" + row.QTYOnOrder + "\t" + row.ItemGenericDescription + "\t" + row.ItemWeight + "\t" + row.ItemShortName + "\t";
                //    streamWriter.WriteLine(strLine);
                //}
                //streamWriter.Close();
                //fileStream.Close();

                FileStream fileStream = new FileStream(FilePath, FileMode.CreateNew, FileAccess.Write);

                ExcelWriter writer = new ExcelWriter(fileStream);
                writer.BeginWrite();
                int k = 0;
                writer.WriteCell(0, k++, "Name");
                writer.WriteCell(0, k++, "Category");
                writer.WriteCell(0, k++, "SubCategory1");
                writer.WriteCell(0, k++, "SubCategory2");
                writer.WriteCell(0, k++, "SubCategory3");
                writer.WriteCell(0, k++, "SubCategory4");
                writer.WriteCell(0, k++, "SubCategory5");
                writer.WriteCell(0, k++, "Manufacturer");
                writer.WriteCell(0, k++, "Description");
                writer.WriteCell(0, k++, "DisplayOrder");
                writer.WriteCell(0, k++, "Price");
                writer.WriteCell(0, k++, "ItemNumber");
                writer.WriteCell(0, k++, "CurrentCost");
                writer.WriteCell(0, k++, "StandardCost");
                writer.WriteCell(0, k++, "ImageName");
                writer.WriteCell(0, k++, "Weight");
                writer.WriteCell(0, k++, "StockAmount");
                writer.WriteCell(0, k++, "PackageQuanity");
                writer.WriteCell(0, k++, "SizeName");
                writer.WriteCell(0, k++, "Diameter1");
                writer.WriteCell(0, k++, "Diameter2");
                writer.WriteCell(0, k++, "Diameter3");
                writer.WriteCell(0, k++, "Diameter4");
                writer.WriteCell(0, k++, "Width1");
                writer.WriteCell(0, k++, "Width2");
                writer.WriteCell(0, k++, "Width3");
                writer.WriteCell(0, k++, "Width4");
                writer.WriteCell(0, k++, "ThreadCount1");
                writer.WriteCell(0, k++, "ThreadCount2");
                writer.WriteCell(0, k++, "ThreadCount3");
                writer.WriteCell(0, k++, "ThreadCount4");
                writer.WriteCell(0, k++, "Length1");
                writer.WriteCell(0, k++, "Length2");
                writer.WriteCell(0, k++, "Length3");
                writer.WriteCell(0, k++, "Length4");
                writer.WriteCell(0, k++, "Featured");
                writer.WriteCell(0, k++, "Active");
                writer.WriteCell(0, k++, "UseDecimalForSize");

                for (int i = 0; i < rows.Count; i++)
                {

                    int j = 0;
                    TblExportImportProduct row = rows[i];
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.Name));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.Category));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.SubCategory1));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.SubCategory2));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.SubCategory3));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.SubCategory4));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.SubCategory5));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.Manufacturer));
                    writer.WriteCell(i + 1, j++, HttpUtility.HtmlDecode(row.Description));
                    writer.WriteCell(i + 1, j++, row.DisplayOrder);
                    writer.WriteCell(i + 1, j++, row.Price.ToString());
                    writer.WriteCell(i + 1, j++, row.ItemNumber);
                    writer.WriteCell(i + 1, j++, row.CurrentCost.ToString());
                    writer.WriteCell(i + 1, j++, row.StandardCost.ToString());
                    writer.WriteCell(i + 1, j++, row.ImageName);
                    writer.WriteCell(i + 1, j++, row.Weight.ToString());
                    writer.WriteCell(i + 1, j++, row.StockAmount);
                    writer.WriteCell(i + 1, j++, row.PackageQuanity);
                    writer.WriteCell(i + 1, j++, row.SizeName);
                    writer.WriteCell(i + 1, j++, row.Diameter1);
                    writer.WriteCell(i + 1, j++, row.Diameter2);
                    writer.WriteCell(i + 1, j++, row.Diameter3);
                    writer.WriteCell(i + 1, j++, row.Diameter4);
                    writer.WriteCell(i + 1, j++, row.Width1);
                    writer.WriteCell(i + 1, j++, row.Width2);
                    writer.WriteCell(i + 1, j++, row.Width3);
                    writer.WriteCell(i + 1, j++, row.Width4);
                    writer.WriteCell(i + 1, j++, row.ThreadCount1);
                    writer.WriteCell(i + 1, j++, row.ThreadCount2);
                    writer.WriteCell(i + 1, j++, row.ThreadCount3);
                    writer.WriteCell(i + 1, j++, row.ThreadCount4);
                    writer.WriteCell(i + 1, j++, row.Length1);
                    writer.WriteCell(i + 1, j++, row.Length2);
                    writer.WriteCell(i + 1, j++, row.Length3);
                    writer.WriteCell(i + 1, j++, row.Length4);
                    writer.WriteCell(i + 1, j++, row.Featured.ToString());
                    writer.WriteCell(i + 1, j++, row.Active.ToString());
                    writer.WriteCell(i + 1, j++, row.UseDecimalForSize.ToString());
                }
                writer.EndWrite();
                fileStream.Close();
            }
            catch (Exception ex) { throw ex; }
        }


        public static void TruncateTable()
        {
            TblExportImportProductExtended.TruncateTable();
        }

        private static int GetCategoryId(TblExportImportProduct data)
        {
            string categoryName;
            if (string.IsNullOrEmpty(data.Category))
            {
                categoryName = data.SubCategory1;
            }
            else
            {
                categoryName = data.SubCategory2;
            }

            ReadWriteCustomCollection<Category> productCategory = CategoryManager.GetBackendCategoryListByNameSearch(categoryName.ToLower(), false, "displayOrder ASC", 0, 99999);
            if (productCategory.Count > 0)
            {
                foreach (Category cat in productCategory)
                {
                    if (cat.CategoryName.ToLower() == categoryName.ToLower())
                    {
                        return cat.CategoryId;
                    }
                }
            }
            return 0;
        }

        //private static int GetManufacturerId(string manufacturerName)
        //{
        //    if (_productBrand == null)
        //    {
        //        _productBrand = ProductBrand.Brands(false);
        //    }

        //    foreach (ProductBrand brand in _productBrand)
        //    {
        //        if (brand.BrandName.ToLower() == manufacturerName.ToLower())
        //        {
        //            return brand.ProductBrandId;
        //        }
        //    }

        //    return 0;
        //}

        private static int CreateManufacturer(TblExportImportProduct data)
        {
            Manufacturer brand = ManufacturerManager.GetProductBrandByName(data.Manufacturer);
            if (brand == null)
            {
                brand = ManufacturerManager.Create(data.Manufacturer, string.Empty, string.Empty, string.Empty, true, false, string.Empty, string.Empty);
            }
            return brand.ProductBrandId;
        }

        //private static void CreateManufacturer(TblExportImportProduct data)
        //{
        //    ProductBrand brand = null;

        //    string brandName = data.Manufacturer;
        //    try
        //    {
        //        ReadWriteCustomCollection<ProductBrand> productBrands = ProductBrand.Brands(false);
        //        if (productBrands != null && productBrands.Count > 0)
        //        {
        //            foreach (ProductBrand productBrand in productBrands)
        //            {
        //                if (productBrand.BrandName.ToLower() == brandName.ToLower() && productBrand.IsActive == true && productBrand.IsDeleted == false)
        //                {
        //                    brand = productBrand;
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    catch { }
        //    if (brand == null)
        //    {
        //        brand = ProductBrand.Create(brandName, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, true, false);
        //    }

        //}

        private static Category FindOrCreateCategory(string categoryName, int parentCategoryId)
        {
            Category category = null;
            category = CategoryManager.FindCategoryByName(parentCategoryId, categoryName.Trim());

            if (category == null)
            {
                category = CategoryManager.Create(categoryName.Trim(), categoryName.Trim(), string.Empty,
                    string.Empty, "1", true, false, parentCategoryId,
                    string.Empty, string.Empty);
            }
            return category;
        }

        private static int CreateCategory(TblExportImportProduct data)
        {
            Category category = null;
            if (!string.IsNullOrEmpty(data.Category))
            {
                category = FindOrCreateCategory(data.Category, SiteUtility.SuperRootCategoryId);
            }
            if (!string.IsNullOrEmpty(data.SubCategory1))
            {
                category = FindOrCreateCategory(data.SubCategory1, category.CategoryId);
            }
            /*if (!string.IsNullOrEmpty(data.SubCategory2))
            {
                category = FindOrCreateCategory(data.SubCategory2, category.CategoryId);
            }
            if (!string.IsNullOrEmpty(data.SubCategory3))
            {
                category = FindOrCreateCategory(data.SubCategory3, category.CategoryId);
            }
            if (!string.IsNullOrEmpty(data.SubCategory4))
            {
                category = FindOrCreateCategory(data.SubCategory4, category.CategoryId);
            }
            if (!string.IsNullOrEmpty(data.SubCategory5))
            {
                category = FindOrCreateCategory(data.SubCategory5, category.CategoryId);
            }*/

            return category.CategoryId;
        }

        //private static void CreateCategory(TblExportImportProduct data)
        //{
        //    ProductCategory parent = null;
        //    ProductCategory child = null;

        //    string categoryName;
        //    categoryName = data.SubCategory2;
        //    try
        //    {
        //        ReadWriteCustomCollection<ProductCategory> productCategory = ProductCategory.GetBackendCategoryListByNameSearch(categoryName.ToLower(), false, "displayOrder ASC", 0, 99999);
        //        if (productCategory != null && productCategory.Count > 0)
        //        {
        //            foreach (ProductCategory cat in productCategory)
        //            {
        //                if (cat.CategoryName.ToLower() == categoryName.ToLower() && cat.IsActive == true && cat.IsDeleted == false)
        //                {
        //                    parent = cat;
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    catch { }
        //    if (parent == null)
        //    {
        //        int parentCategoryId = SiteUtility.SuperRootCategoryId;
        //        parent = ProductCategory.Create(categoryName, categoryName, string.Empty, string.Empty, string.Empty, true, false, parentCategoryId, string.Empty, string.Empty);
        //    }

        //    categoryName = data.SubCategory1;
        //    try
        //    {
        //        ReadWriteCustomCollection<ProductCategory> subCategories = ProductCategory.GetSubCategories(parent);
        //        if (subCategories != null && subCategories.Count > 0)
        //        {
        //            foreach (ProductCategory cat in subCategories)
        //            {
        //                if (cat.CategoryName.ToLower() == categoryName.ToLower() && cat.IsActive == true && cat.IsDeleted == false)
        //                {
        //                    child = cat;
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    catch { }
        //    if (child == null)
        //    {
        //        int parentCategoryId = parent.CategoryId;
        //        child = ProductCategory.Create(categoryName, categoryName, string.Empty, string.Empty, string.Empty, true, false, parentCategoryId, string.Empty, string.Empty);
        //    }
        //}

    }

}
